package com.neuedu.elm.view;

import com.neuedu.elm.dao.IAdminDao;
import com.neuedu.elm.dao.IBusinessDao;
import com.neuedu.elm.dao.IFoodDao;
import com.neuedu.elm.dao.Impl.AdminDaoImpl;
import com.neuedu.elm.dao.Impl.BusinessDaoImpl;
import com.neuedu.elm.dao.Impl.FoodDaoImpl;
import com.neuedu.elm.po.Admin;
import com.neuedu.elm.po.Business;
import com.neuedu.elm.po.Food;

import java.util.List;
import java.util.Scanner;

/*将重复代码提取成方法：程序中有很多重复的代码逻辑，比如 Scanner 的使用、菜单选项的显示等，可以将这些逻辑提取成独立的方法，减少代码冗余。

减少方法的责任：一些方法的功能比较复杂，应该将其拆分成多个方法，使每个方法只负责一个特定的功能，遵循单一责任原则。

使用工厂模式或依赖注入：可以考虑使用工厂模式或依赖注入来创建 DAO 实例，以减少 new 操作的耦合性。

使用枚举或常量来表示菜单选项：避免使用硬编码的整数值来表示菜单选项，使用枚举或常量来增强代码的可读性和可维护性。

优化业务逻辑：在某些情况下可以考虑将业务逻辑放入服务层，以便于在将来扩展和维护。*/

//这是ChatGpt优化版（仅供学习和参考代码结构和语法）
public class NewManagerEntry {

    private static final Scanner scanner = new Scanner(System.in);
    private static final IAdminDao adminDao = new AdminDaoImpl();
    private static final IBusinessDao businessDao = new BusinessDaoImpl();
    private static final IFoodDao foodDao = new FoodDaoImpl();

    public static void main(String[] args) {
        managerView();
    }

    private static void managerView() {
        System.out.println("欢迎来到该程序，请选择商家登录和管理员登陆");
        System.out.println("管理员登录输入1，商家登录输入2");

        int choice = scanner.nextInt();
        if (choice == 1) {
            handleAdminLogin();
        } else if (choice == 2) {
            handleBusinessLogin();
        } else {
            System.err.println("无效的选项，请重试！");
        }
    }

    private static void handleAdminLogin() {
        System.out.println("请输入管理员账号:");
        String name = scanner.next();
        System.out.println("请输入管理员密码:");
        String password = scanner.next();

        Admin admin = adminDao.login(name, password);
        if (admin != null && admin.getAdminName() != null) {
            adminMenu(admin);
        } else {
            System.err.println("登陆失败，账号或密码错误");
        }
    }

    private static void adminMenu(Admin admin) {
        System.out.println("欢迎 " + admin.getAdminName() + " 访问该程序");
        while (true) {
            System.out.println("输入数字执行操作：1.查询商家列表 2.查询商家 3.新建商家 4.删除商家 5.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    showBusinessViewBy();
                    break;
                case 2:
                    showBusinessViewBy();//其实就是缩减成一个了·
                    break;
                case 3:
                    Business.insertBusiness();
                    break;
                case 4:
                    deleteBusinessView();
                    break;
                case 5:
                    return;
                default:
                    System.err.println("无效的选项，请重试！");
            }
        }
    }

    private static void handleBusinessLogin() {
        System.out.println("请输入商家账号:");
        String name = scanner.next();
        System.out.println("请输入商家密码:");
        String password = scanner.next();

        Business business = businessDao.login(name, password);
        if (business != null && business.getBusinessName() != null) {
            businessMenu(business);
        } else {
            System.err.println("登陆失败，账号或密码错误");
        }
    }

    private static void businessMenu(Business business) {
        System.out.println("欢迎 " + business.getBusinessName() + " 访问该程序");
        while (true) {
            System.out.println("输入数字执行操作：1.查询商家信息 2.修改商家信息 3.修改商家密码 4.查询菜品信息 5.添加菜品信息 6.修改菜品信息 7.删除菜品信息 8.退出");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    queryBusinessView(business.getBusinessId());
                    break;
                case 2:
                    updateBusinessView(business.getBusinessId());
                    break;
                case 3:
                    updateBusinessPwdView(business.getBusinessId());
                    break;
                case 4:
                    findAllFood(business.getBusinessId());
                    break;
                case 5:
                    addFood(business.getBusinessId());
                    break;
                case 6:
                    updateFood(business.getBusinessId());
                    break;
                case 7:
                    deleteFood(business.getBusinessId());
                    break;
                case 8:
                    return;
                default:
                    System.err.println("无效的选项，请重试！");
            }
        }
    }

    //3.条件搜索商家
    private static void showBusinessViewBy() {
        System.out.println("是否按照名字查询? Y/N");
        String name = scanner.next().equalsIgnoreCase("Y") ? getInput("请输入商家名字") : null;

        System.out.println("是否按照地址查询? Y/N");
        String address = scanner.next().equalsIgnoreCase("Y") ? getInput("请输入商家地址") : null;

        List<Business> businessList = businessDao.selectBusiness(name, address);
        businessList.forEach(System.out::println);
    }

    //删除商家
    private static void deleteBusinessView() {
        int id = getIntInput("请输入商家id");
        if (businessDao.deleteBusiness(id) != 0) {
            System.out.println("删除成功");
        } else {
            System.err.println("删除失败，该id可能不存在");
        }
    }

    //查询商家信息
    private static void queryBusinessView(int id) {
        System.out.println("您的信息是:");
        Business business = businessDao.queryBusiness(id);
        System.out.println(business.toString1());
    }

    //修改商家信息
    private static void updateBusinessView(int id) {
        Business business = new Business();
        business.setBusinessId(id);
        business.setBusinessName(getUpdateInput("名称"));
        business.setBusinessAddress(getUpdateInput("地址"));
        business.setBusinessExplain(getUpdateInput("描述"));
        business.setStarPrice(getUpdateFloatInput("起送费"));
        business.setDeliveryPrice(getUpdateFloatInput("配送费"));
        businessDao.updateBusiness(business);
    }

    //修改密码
    private static void updateBusinessPwdView(int businessId) {
        String newPwd = getInput("请输入要修改的密码");
        if (!newPwd.equals(getInput("请再次输入要修改的密码"))) {
            System.out.println("两次密码不相同，请重新操作");
        } else {
            businessDao.updateBusinessPwd(businessId, newPwd);
        }
    }

    //查询菜品信息
    private static void findAllFood(int id) {
        System.out.println("正在查询菜品信息");
        foodDao.findAllFood(id);
    }

    //添加菜品信息
    private static void addFood(int id) {
        Food food = new Food();
        food.setFoodName(getInput("请输入所添加的菜品名称"));
        food.setFoodExplain(getInput("请输入所添加的菜品描述"));
        food.setFoodPrice(getFloatInput("请输入所添加的菜品价格"));
        food.setBusinessId(id);
        if (foodDao.addFood(food) != 0) {
            System.out.println("添加成功");
        }
    }

    //修改菜品信息
    private static void updateFood(int id) {
        Food food = new Food();
        String name = getInput("请输入菜品名称(我们将修改这个菜品的信息)");
        food.setFoodName(getUpdateInput("菜品名称"));
        food.setFoodExplain(getUpdateInput("菜品描述"));
        food.setFoodPrice(getUpdateFloatInput("菜品价格"));
        food.setBusinessId(id);
        if (foodDao.updateFood(food, name) != 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("亲，十分抱歉，库中没有您说的菜品");
        }
    }

    //删除菜品
    private static void deleteFood(int id) {
        String name = getInput("请输入想要删除的商品 ");
        if (confirmAction("您确定真的要删除 " + name + " ?")) {
            Food food = new Food();
            food.setBusinessId(id);
            food.setFoodName(name);
            if (foodDao.deleteFood(food) != 0) {
                System.out.println("菜品 " + name + " 删除成功");
            } else {
                System.out.println("菜品删除失败，您要删除的 " + name + " 可能不存在");
            }
        }
    }

    // 辅助方法：用于获取输入
    private static String getInput(String prompt) {
        System.out.println(prompt);
        return scanner.next();
    }

    private static int getIntInput(String prompt) {
        System.out.println(prompt);
        return scanner.nextInt();
    }

    private static float getFloatInput(String prompt) {
        System.out.println(prompt);
        return scanner.nextFloat();
    }

    private static String getUpdateInput(String field) {
        System.out.println("是否修改" + field + "? Y/N");
        return scanner.next().equalsIgnoreCase("Y") ? getInput("请输入要修改的" + field) : null;
    }

    private static Float getUpdateFloatInput(String field) {
        System.out.println("是否修改" + field + "? Y/N");
        return scanner.next().equalsIgnoreCase("Y") ? getFloatInput("请输入要修改的" + field) : null;
    }

    private static boolean confirmAction(String prompt) {
        System.out.println(prompt + " Y/N");
        return scanner.next().equalsIgnoreCase("Y");
    }
}
